package constant.milk.periodapp.tip;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import constant.milk.periodapp.R;
import constant.milk.periodapp.SuperActivity;

public class TipActivity extends SuperActivity
{
	private TextView backTextView;
	
	private ListView listView;
	
	private ArrayList<String> list;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.tip_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		backTextView = (TextView) findViewById(R.id.tipBackTextView);
		
		listView = (ListView) findViewById(R.id.tipListView);
	}

	@Override
	protected void setOnClick()
	{
		backTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
	}

	@Override
	protected void setData()
	{
		TipArray ta = new TipArray();
		list = ta.getList();
		
		listView.setOnItemClickListener(oicl);
		TipAdapter adapter = new TipAdapter(superContext, R.layout.list_item_tip, list);
		listView.setAdapter(adapter);
		adapter.notifyDataSetChanged();
	}
	
	private OnItemClickListener oicl = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id)
		{
			Intent intent = new Intent(superContext, TipInfoActivity.class);
			intent.putExtra(TipInfoActivity.TIP_ID, position);
			startActivity(intent);
		}
	};
	
	private class TipAdapter extends ArrayAdapter<String>
	{
		public TipAdapter(Context context, int resource, List<String> objects)
		{
			super(context, resource, objects);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null)
			{
				LayoutInflater li = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(R.layout.list_item_tip, null);
			}
			
			String data = getItem(position);
			
			TextView tv = (TextView) v.findViewById(R.id.listItemTipTextView);
			
			tv.setText(data);
			
			return v;
		}
	}
}
